<template>
  <d2-container>
    <div class="mod-config">
      <div class="search">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)" size="small">
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-input v-model="dataForm.key" placeholder="采集设备名称" clearable></el-input>
          </el-form-item>
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-button @click="getDataList(true)">查询</el-button>
            <el-button v-if="isAuth('ecs:ecscollectequi:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button v-if="isAuth('ecs:ecscollectequi:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;margin-top:15px;">
        <!-- <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column> -->
        <el-table-column
          prop="equiCode"
          header-align="center"
          align="center"
          label="采集设备编码">
            <!-- <template slot-scope="scope">
              <span class="link" @click="viewDetail(scope.row)">{{scope.row.equiCode}}</span>
            </template> -->
        </el-table-column>
        <el-table-column
          prop="equiName"
          header-align="center"
          align="center"
          label="采集设备名称">
           <template slot-scope="scope">
              <span class="link" @click="viewDetail(scope.row)">{{scope.row.equiName}}</span>
            </template>
        </el-table-column>
        <el-table-column
          prop="equiIp"
          header-align="center"
          align="center"
          label="IP">
        </el-table-column>
        <el-table-column
          prop="equiPort"
          header-align="center"
          align="center"
          label="端口">
        </el-table-column>
        <!-- <el-table-column
          prop="equiAddress"
          header-align="center"
          align="center"
          min-width="180"
          label="采集设备安装地址">
        </el-table-column> -->
        <el-table-column
          prop="sn"
          header-align="center"
          align="center"
          label="设备序列号">
        </el-table-column>
        <!-- <el-table-column
          prop="equiLastupdate"
          header-align="center"
          align="center"
          min-width="180"
          label="采集设备更新时间">
        </el-table-column> -->
        <el-table-column
          prop="buildingId"
          header-align="center"
          min-width="center"
          label="区域编号">
        </el-table-column>
        <el-table-column
          prop="gatewayId"
          header-align="center"
          align="center"
          label="网关号">
        </el-table-column>
        <!-- <el-table-column
          prop="configVer"
          header-align="center"
          min-width="center"
          width="120"
          label="数据版本号">
        </el-table-column>
        <el-table-column
          prop="configContent"
          header-align="center"
          align="center"
          min-width="120"
          label="数据内容">
        </el-table-column> -->
        <el-table-column
          header-align="center"
          align="center"
          min-width="230"
          label="操作">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="addOrUpdateHandle(scope.row.equiCode)">修改</el-button>
            <!-- <el-button type="primary" size="small" @click="syncMeterInfo(scope.row.equiCode)">读取</el-button> -->
            <el-button type="warning" size="small" @click="deleteHandle(scope.row.equiCode)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-4"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <view-detail ref="viewDetailComponent"  v-show="viewVisible" :equiData="viewData" @closeView="closeView()"></view-detail>
    </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './ecscollectequi-add-or-update'
  import viewDetail from './ecscollectequi-view'
  import * as Util from "@/utils";
  export default {
    name: 'ecs_ecscollectequi',
    data () {
      return {
        viewVisible:false,
        viewData:{},
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      viewDetail,
    },
    activated () {
      this.getDataList()
    },
    computed:{
      containerHeight() {
        return Util.getClientHeight() - 250;
      }
    },
    methods: {
      closeView(){
        this.viewVisible = false;
      },
      //查看详情
      viewDetail(row){
        this.viewData = row;
        this.viewVisible = true;
        this.$refs.viewDetailComponent.showDialog();
      },
      //同步计量器信息
      syncMeterInfo(code){

      },
      // 获取数据列表
      getDataList (type) {
        if(type){
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$api.okhttp({
          url: '/ecs/ecscollectequi/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            "customerId": this.$util.cookies.get('customer_id')
          }
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.equiCode
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: '/ecs/ecscollectequi/delete',
            method: 'post',
            data: {}
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
